package com.tshopping.service.impl;

import com.tshopping.bean.*;
import com.tshopping.dao.GoodsDao;
import com.tshopping.dao.LevelDao;
import com.tshopping.dao.ParameterDao;
import com.tshopping.dao.PictureDao;
import com.tshopping.service.GoodsService;
import com.tshopping.service.LevelService;
import com.tshopping.util.FileUploader;
import com.tshopping.util.ResponseData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @name: GoodsServiceImpl
 * @description: 商品业务层类
 * @author: Jackeys
 * @date: 2021-01-23 21:32
 */
@Service(value = "goodsService")
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private ParameterDao parameterDao;

    @Autowired
    private PictureDao pictureDao;

    @Autowired
    private LevelDao levelDao;

    @Override
    /**
     * @description 处理用户添加商品
     * @Param: name
     * @Param: description
     * @Param: cover
     * @Param: price
     * @Param: stock
     * @Param: user
     * @Param: level
     * @Param: freight
     * @Param: store
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-23 21:33
     */
    public ResponseData addGoods(DataModel dataModel) {
        ResponseData responseData = ResponseData.ok();
        Goods goods = dataModel.getGoods();
        if (goodsDao.insertGoodsByGoodsInformation(goods)) {
            if (goods.getId() > 0) {
                /*商品参数*/
                for (Parameter parameter : dataModel.getParameters()) {
                    if (!parameterDao.insertParameterByParameterInformation(parameter.getStock(), parameter.getSpecification(), goods.getId(), parameter.getColor())) {
                        responseData.putDataValue("flag", false);
                        return responseData;
                    }
                }
                /*商品参数*/
                for (String picture : dataModel.getPictures()) {
                    if (!pictureDao.insertGoodsPictures(goods.getId(), picture)) {
                        responseData.putDataValue("flag", false);
                        return responseData;
                    }
                }
                responseData.putDataValue("flag", true);
                return responseData;
            } else {
                responseData.putDataValue("flag", false);
                return responseData;
            }
        }
        responseData.putDataValue("flag", false);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户获取商品
     * @Param: store
     * @Param: user
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-23 21:35
     */
    public ResponseData getGoods(int store, int user,int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = goodsDao.selectCountByStoreAndUser(store, user);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("page", page);
        responseData.putDataValue("goodsList", goodsDao.selectGoodsByStoreIDAndUserID(store, user, start, size));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户上下架商品
     * @Param: isSell
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-23 21:36
     */
    public ResponseData checkGoods(int isSell, int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", goodsDao.updateGoodsIsSellById(isSell, id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户修改商品信息
     * @Param: name
     * @Param: description
     * @Param: cover
     * @Param: price
     * @Param: stock
     * @Param: level
     * @Param: freight
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-23 21:37
     */
    public ResponseData updateGoods(DataModel dataModel) {
        ArrayList<Parameter> parameters = dataModel.getParameters();
        ArrayList<String> pictures = dataModel.getPictures();
        ResponseData responseData = ResponseData.ok();
        if (!goodsDao.updateGoodsById(dataModel.getGoods())) {
            responseData.putDataValue("flag", false);
            return responseData;
        }
        int goodsId = dataModel.getGoods().getId();
        if (!parameterDao.deleteParameterByGoodsId(goodsId)) {
            responseData.putDataValue("flag", false);
            return responseData;
        }
        for (Parameter parameter : parameters) {
            if (!parameterDao.insertParameterByParameterInformation(parameter.getStock(), parameter.getSpecification(), goodsId, parameter.getColor())) {
                responseData.putDataValue("flag", false);
                return responseData;
            }
        }
        if (!pictureDao.deletePicturesByGoodsId(goodsId)) {
            responseData.putDataValue("flag", false);
            return responseData;
        }
        for (String picture : pictures) {
            if (!pictureDao.insertGoodsPictures(goodsId, picture)) {
                responseData.putDataValue("flag", false);
                return responseData;
            }
        }
        responseData.putDataValue("flag", true);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户删除商品
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-23 21:39
     */
    public ResponseData deleteGoods(int id) {
        ResponseData responseData=ResponseData.ok();
        if (!goodsDao.deleteGoodsById(id)) {
            responseData.putDataValue("flag",false);
            return responseData;
        }
        if (!pictureDao.deletePicturesByGoodsId(id)) {
            responseData.putDataValue("flag",false);
            return responseData;
        }
        if (!parameterDao.deleteParameterByGoodsId(id)) {
            responseData.putDataValue("flag",false);
            return responseData;
        }
        responseData.putDataValue("flag",true);
        return responseData;
    }

    @Override
    /**
     * @description 返回所有商品
     * @Param:
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-23 21:40
     */
    public ResponseData selectAllGoods() {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("goodsList",goodsDao.selectAllGoods());
        return responseData;
    }

    @Override
    /**
     * @description 上传图片
     * @Param: request
     * @Param: email
     * @Param: file
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-24 12:36
     */
    public ResponseData uploadGoodsPicture(HttpServletRequest request, String email, MultipartFile file) {
        ResponseData responseData=ResponseData.ok();
        String directoryPath = request.getSession().getServletContext().getRealPath("resource/users/" + email + "/goodsPicture/");
        String originalFileName = file.getOriginalFilename();
        String newFileName = UUID.randomUUID() + originalFileName;
        String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/users/" + email + "/goodsPicture/" + newFileName;
        if(FileUploader.upload(directoryPath, newFileName, file)) {
            responseData.putDataValue("pictureURL",path);
        } else {
            responseData.putDataValue("pictureURL",null);
        }
        return responseData;
    }

    @Override
    /**
     * @description 用户获取商品
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-25 09:37
     */
    public ResponseData selectGoods(int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("goods",goodsDao.selectGoodsByGoodsId(id));
        responseData.putDataValue("parameterList",parameterDao.selectParametersByGoodsId(id));
        responseData.putDataValue("pictureList",pictureDao.selectPicturesByGoods(id));
        Goods goods= (Goods) responseData.getData().get("goods");
        responseData.putDataValue("level",levelDao.selectLevelByLevelId(goods.getLevel()));
        return responseData;
    }

    @Override
    /**
     * @description 返回所有用户
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-02-01 15:37
     */
    public ResponseData getAllGoods(int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = goodsDao.selectAllGoodsCount();
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("page", page);
        responseData.putDataValue("goodsList", goodsDao.getAllGoods(start,size));
        return responseData;
    }

    @Override
    /**
     * @description 审核商品
     * @Param: isApprove
     * @Param: id
     * @return  ResponseData
     * @author Jackeys
     * @date 2021-02-01 15:43
     */
    public ResponseData isApprove(int isApprove, int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag",goodsDao.updateIsApprove(isApprove,id));
        return responseData;
    }
}
